//
//  GiftViewController.m
//  LOL_JewelBox
//
//  Created by dllo on 15/4/24.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "GiftViewController.h"
#import "AFHTTPRequestOperationManager.h"
#import "UIImageView+WebCache.h"
#import "ToolsModel.h"
#import "ToolsView.h"
#import "GiftsModel.h"
#import "DetailNewsCell.h"
#import "DetailCollectionCell.h"
@interface GiftViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>

@property(nonatomic, retain)ToolsView *toolsView;
@property(nonatomic, retain)UICollectionView *collectionView;
@property(nonatomic, retain)UISegmentedControl *seg;
@property(nonatomic, retain)NSMutableArray *transformArr;
@property(nonatomic, retain)NSMutableArray *giftArr;
@property(nonatomic, retain)NSMutableArray *levelArr;
@property(nonatomic, retain)NSMutableArray *attackArr;  // 攻击天赋
@property(nonatomic, retain)NSMutableArray *defenseArr; // 防御天赋
@property(nonatomic, retain)NSMutableArray *generalArr; // 通用天赋

@end

@implementation GiftViewController

- (void)dealloc
{
    
    [_toolsView release];
    [_collectionView release];
    [_seg release];
    [_transformArr release];
    [_giftArr release];
    [_levelArr release];
    [_attackArr release];
    [_defenseArr release];
    [_generalArr release];
    [super dealloc];
    
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = @"天赋";
        self.transformArr = [NSMutableArray array];
        self.giftArr = [NSMutableArray array];
        self.levelArr = [NSMutableArray array];
        self.attackArr = [NSMutableArray array];
        self.defenseArr = [NSMutableArray array];
        self.generalArr = [NSMutableArray array];
    }
    return self;
}

// 数据解析
- (void)giftData
{   [self.transformArr removeAllObjects];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:@"http://lolbox.duowan.com/phone/apiGift.php?OSType=iOS7.1.1&v=70" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSMutableDictionary *dic = responseObject;
        NSMutableArray *attactArr = dic[@"a"];
        NSMutableArray *defenceArr = dic[@"d"];
        NSMutableArray *generalArr = dic[@"g"];
        
        
        for (NSMutableDictionary *tempDic in attactArr) {
            GiftsModel *levelModel = [[GiftsModel alloc] init];
            [levelModel setValuesForKeysWithDictionary:tempDic];
            [self.attackArr addObject:levelModel];
            [self.transformArr addObject:levelModel];
            
            [levelModel release];
        }
        GiftsModel *giftAttack = [[GiftsModel alloc] init];
        [self.transformArr insertObject: giftAttack atIndex:18];
        [self.transformArr insertObject:giftAttack atIndex:20];
        [giftAttack release];
        self.seg.selectedSegmentIndex = 0;
        [self.collectionView reloadData];
        
        
        for (NSMutableDictionary *tempDic in defenceArr) {
            GiftsModel *levelModel = [[GiftsModel alloc] init];
            [levelModel setValuesForKeysWithDictionary:tempDic];
            [self.defenseArr addObject:levelModel];
            [levelModel release];
        }
       
        for (NSMutableDictionary *tempDic in generalArr) {
            GiftsModel *levelModel = [[GiftsModel alloc] init];
            [levelModel setValuesForKeysWithDictionary:tempDic];
            [self.generalArr addObject:levelModel];
            [levelModel release];
        }
        



    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self giftData];
    
    // 铺设自定义View
    self.toolsView = [[ToolsView alloc] initWithFrame:CGRectMake(0, 0, VIEWWIDTH, VIEWHEIGHT)];
    self.toolsView.backgroundColor = [UIColor blackColor];
    [self.view addSubview:self.toolsView];
    [_toolsView release];
    
    // 铺设一个Collection
    // 创建一个layout
    UICollectionViewFlowLayout *flovLayout = [[UICollectionViewFlowLayout alloc] init];
    // 图片大小
    flovLayout.itemSize = CGSizeMake(VIEWWIDTH / 5.357, VIEWHEIGHT / 8.893);
    // 最小行列间距
    flovLayout.minimumInteritemSpacing = 1;
    flovLayout.minimumLineSpacing = (VIEWWIDTH / 5.357) / 4;
    
    // 设置item的边框
    flovLayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(5, 35, VIEWWIDTH - 10, VIEWHEIGHT - 96 - 49) collectionViewLayout:flovLayout];
    
    // 设置代理人
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.backgroundColor = [UIColor colorWithRed:218 / 255.0 green:218 / 255.0 blue:218 / 255.0 alpha:1];
    [self.toolsView addSubview:self.collectionView];
    [_collectionView release];
    
    
    // 设置装备选择界面
    self.seg = [[UISegmentedControl alloc] initWithItems:@[@"攻 击", @"防 御", @"通 用"]];
    self.seg.frame = CGRectMake(0, 0, VIEWWIDTH, 32);
    self.seg.backgroundColor = [UIColor blackColor];
    [self.seg addTarget:self action:@selector(change:) forControlEvents:UIControlEventValueChanged];
    [self.toolsView addSubview:self.seg];
    [_seg release];
    
    // 注册方法
    [self.collectionView registerClass:[DetailCollectionCell class] forCellWithReuseIdentifier:@"reuse"];
    
    
}

// collectionView的两个协议方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.transformArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    DetailCollectionCell *detailcell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    GiftsModel *giftModel = self.transformArr[indexPath.row];
    detailcell.detailName.text = giftModel.name;
    
    NSString *png = [NSString stringWithFormat:@"http://img.lolbox.duowan.com/gifts/%@.png", giftModel.id];
    NSURL *url = [NSURL URLWithString:png];
    [detailcell.detailPng sd_setImageWithURL:url];
    
    return detailcell;
}

// 天赋分类以及各类型的铺设
- (void)change:(UISegmentedControl *)sander
{
    [self.transformArr removeAllObjects];
    NSInteger index = self.seg.selectedSegmentIndex;
    switch (index) {
        case 0:
            for (GiftsModel *temp in self.attackArr) {
                [self.transformArr addObject:temp];
                
            }
            if (self.attackArr.count != 0) {
                GiftsModel *giftAttack = [[GiftsModel alloc] init];
                [self.transformArr insertObject: giftAttack atIndex:18];
                [self.transformArr insertObject:giftAttack atIndex:20];
                [giftAttack release];
                
                [self.collectionView reloadData];
            }
            
            break;
        case 1:
            for (GiftsModel *temp in self.defenseArr) {
                [self.transformArr addObject:temp];
                
            }
            if (self.defenseArr.count != 0) {
                GiftsModel *giftDefense = [[GiftsModel alloc] init];
                [self.transformArr insertObject:giftDefense atIndex:6];
                [self.transformArr insertObject:giftDefense atIndex:19];
                [self.transformArr insertObject:giftDefense atIndex:20];
                [giftDefense release];
                
                [self.collectionView reloadData];
            }
            
            break;
        case 2:
            for (GiftsModel *temp in self.generalArr) {
                [self.transformArr addObject:temp];
                
            }
            if (self.generalArr.count != 0) {
                GiftsModel *giftGeneral = [[GiftsModel alloc] init];
                [self.transformArr insertObject:giftGeneral atIndex:4];
                [self.transformArr insertObject:giftGeneral atIndex:16];
                [self.transformArr insertObject:giftGeneral atIndex:19];
                [self.transformArr insertObject:giftGeneral atIndex:20];
                [giftGeneral release];
                
                [self.collectionView reloadData];
            }
            
            break;
        default:
            break;
    }
   
}

// 点击事件
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{     NSString *temp = @"";
    GiftsModel *model = self.transformArr[indexPath.row];
    for (NSInteger i = 0; i < model.level.count; i++) {
        
      temp = [NSString stringWithFormat:@"%@%@\n",temp,model.level[i]];
        
    }
    
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:temp message:nil delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
    [alert show];
    [alert release];
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
